////
/// Heading
/// @group Heading
////

//  th-heading()
/// Output styles for a heading list.
///
/// @since 0.0.10
///
/// @param {string} $heading - A heading map key.
/// @param {number | boolean} $breakpoint [false] - A heading list breakpoint.
/// @param {number} $base-font-size [$th-base-font-size] - Font size used for relative calculations.
/// @param {list | boolean} $include [false] - (font-size | line-height | margin-top | margin-bottom) Restrict output properties.
///
/// @example scss Output h1 styles.
///   th-heading(h1)
/// @example scss - Output h1 styles with 768px breakpoint.
///   th-heading(
///     $heading: h1,
///     $breakpoint: 768px
///   )
/// @example scss - Output h1 styles with a base font size of 48px.
///   th-heading(
///     $heading: h1,
///     $base-font-size: 48px
///   )
/// @example scss - Output h1 styles but only include font-size and line-height.
///   th-heading(
///     $heading: h1,
///     $include: (font-size line-height)
///   )

@mixin th-heading(
  $heading,
  $breakpoint: false,
  $base-font-size: $th-base-font-size,
  $include: (font-size line-height margin-top margin-bottom)
){
  $heading: th-heading($heading, $breakpoint);
  $font-size: th-property(
    $property-name: font-size,
    $heading: $heading,
    $base-font-size: $base-font-size,
    $convert: false
  );
  $properties: th-list-flatten($_th-core-heading-structure);
  @each $property in $properties {
    @if not $include or th-list-has($include, $property) {
      @include th-property(
        $property-name: $property,
        $heading: $heading,
        $base-font-size: th-if(font-size == $property, $base-font-size, $font-size)
      );
    }
  }
}

//  th-headings
/// Output styles for a heading across all breakpoints.
///
/// @since 0.0.10
///
/// @param {list | string} $heading - A heading map key or list.
/// @param {number} $base-font-size ($th-base-font-size) Font size used for relative calculations.
/// @param {list | boolean} $include [false] - (font-size | line-height | margin-top | margin-bottom) Restrict output properties.
///
/// @example scss - Output all h1 styles across all breakpoints.
///   @include th-headings(h1)
/// @example scss - Output font-size and line-height h1 styles across all breakpoints.
///   th-headings(
///     $heading: h1,
///     $include: (font-size line-height)
///   )

@mixin th-headings(
  $heading,
  $base-font-size: $th-base-font-size,
  $include: (font-size line-height margin-top margin-bottom)
){
  @include th-heading-context($heading, true) {
    $heading: th-core-context-get(heading);
    @include th-heading(
      $heading: $heading,
      $base-font-size: $base-font-size,
      $include: $include
    );
  }
}

//  th-heading-context
/// Output content with heading context.
///
/// @since 0.0.15
/// @content [Output with heading context]
///
/// @param {list | string} $heading - A heading map key or list.
///
/// @example scss - Output a heading with a 768px breakpoint.
///   @include th-heading-context(h1) {
///     $heading: th-core-context-get(heading);
///     @include th-heading($heading);
///   }

@mixin th-heading-context(
  $heading,
  $breakpoint-output: false
){

  $heading: th-heading-get-map($heading);
  @if _th-heading-has-next($heading) {
    @include _th-heading-context-loop($heading, $breakpoint-output) {
      @include th-core-context-set(
        heading, th-heading(
          $heading: $heading,
          $breakpoint: th-core-context-get(breakpoint)
        )
      ){
        @content;
      }
    }
  } @else {
    @include th-core-context-set(heading, $heading) {
      @content;
    }
  }
}

// _th-heading-context-loop
/// Loops through a heading map and outputs @content with breakpoint context.
///
/// @since 0.0.14
/// @access private
/// @content
///
/// @param {type} $name
/// @param {list | string} $heading - A heading map key or list.

@mixin _th-heading-context-loop(
  $heading,
  $breakpoint-output: false
){
  $heading-map: th-heading-get-map($heading);
  @if _th-heading-has-next($heading) {
    @each $heading-list in $heading-map {
      $breakpoint: _th-property-get($heading-list, breakpoint);
      @if $breakpoint {
        @if $breakpoint-output {
          @include th-breakpoint($breakpoint, $breakpoint) {
            @content;
          }
        } @else {
          @include th-breakpoint-context($breakpoint) {
            @content;
          }
        }
      } @else {
        @content;
      }
    }
  }
}

//  th-h()
/// Alias to th-heading() mixin.
///
/// @since 0.0.15

@mixin th-h(
  $heading,
  $breakpoint: false,
  $base-font-size: $th-base-font-size,
  $include: (font-size line-height margin-top margin-bottom)
){
  @include th-heading(
    $heading: $heading,
    $breakpoint: $breakpoint,
    $base-font-size: $base-font-size,
    $include: $include
  );
}

//  th-h()
/// Alias to th-headings() mixin.
///
/// @since 0.0.15

@mixin th-hs(
  $heading,
  $base-font-size: $th-base-font-size,
  $include: (font-size line-height margin-top margin-bottom)
){
  @include th-headings(
    $heading: $heading,
    $base-font-size: $base-font-size,
    $include: $include
  );
}